﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using Newtonsoft.Json;
using BySoft.Server.Business;

namespace BySoft.Web.Site.UserControl.SuggestBox
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class JquerySuggest : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["term"] != null && context.Request.QueryString["term"] != "")
            {
                context.Response.Clear();
                context.Response.Charset = "utf-8";
                context.Response.Buffer = true;
                context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.ContentType = "application/json";
                context.Response.Write(GetJosonData(context.Request.QueryString["term"], context.Request.QueryString["contype"]));
                context.Response.Flush();
                //context.Response.Close();
                context.Response.End();
            } 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public DataTable GetSupplierData(string keyWords)
        {
            string sqlstr = " SELECT SupplierID as _ID,SupplierName as _Name FROM MD_Supplier WHERE Status='1' and (SupplierID LIKE '%" + keyWords + "%' OR SupplierName LIKE '%" + keyWords + "%') ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

        public DataTable GetMaterialData(string keyWords)
        {
            string sqlstr = @"  SELECT top 10 a.MaterialID as _ID ,a.PictureNum AS _Name,a.Thickness AS _Thick,a.OutlineSize AS _Size 
  FROM MD_Material a
 LEFT JOIN SM_StockManage b ON a.MaterialID=b.PartsID
WHERE Status='1' AND b.TotalWeight>0 and (a.PictureNum LIKE '%" + keyWords + "%' OR a.MaterialName LIKE '%" + keyWords + "%' ) ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

        public DataTable GetStockData(string keyWords)
        {
            string sqlstr = " SELECT StockID as _ID,StockName as _Name FROM MD_Stock WHERE Status='1' and (StockName LIKE '%" + keyWords + "%') ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

        public DataTable GetOrderData(string keyWords)
        {
            string sqlstr = @" SELECT bo.OrderID as _Name, ms.SuppliersName as _ID
FROM BS_Order bo
LEFT JOIN MD_Suppliers ms ON ms.SuppliersID=bo.SuppliersID
WHERE bo.[Status]='2' and (bo.OrderID LIKE '%" + keyWords + "%' ) ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

        public DataTable GetUserData(string keyWords)
        {
            string sqlstr = @" SELECT UserID as _ID,  UserName as _Name FROM SYS_User 
WHERE Status='1' and (UserName LIKE '%" + keyWords + "%' OR UserID LIKE '%" + keyWords + "%') ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

        public DataTable GetRequisit(string keyWords)
        {
            string sqlstr = @" SELECT RequisitionID as _ID,  RequisitionID as _Name FROM CG_Requisition 
WHERE Status='1' and RequisitionID LIKE '%" + keyWords + "%' ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }

       
          public DataTable GetLine(string keyWords)
        {
            string sqlstr = @" SELECT LineNameID as _ID,  LineName as _Name FROM MD_LineName 
WHERE Status='1' and LineName LIKE '%" + keyWords + "%' ORDER BY _Name";
            return Common.ExecuteDataTable(sqlstr, 10);

        }
        public string GetJosonData(string keyWords,string type)
        {
            keyWords.Replace("'", "''").Replace("%", "[%]").ToLower();
            DataTable dt = new DataTable();
            switch (type)
            {
                case "2"://供应商表
                    dt = GetSupplierData(keyWords);
                    break;
                case "1"://项目 工令号
                    dt = GetOrderData(keyWords);
                    break;
                case "3"://用户
                    dt = GetUserData(keyWords);
                    break;
                case "4"://用户
                    dt = GetRequisit(keyWords);
                    break;
                case "11"://线路
                    dt = GetLine (keyWords);
                    break;
                default:
                    break;
            }
           
            return JsonConvert.SerializeObject(dt);
        }   
    }
}
